﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace cbelo.Models
{
    public class ShopCart
    {
        cbeloEntities connection = new cbeloEntities();

        string ShoppingCartId { get; set; }

        public const string CartSessionKey = "BeloCartId";

        public static ShopCart GetCart(HttpContextBase context)
        {
            var cart = new ShopCart();
            cart.ShoppingCartId = cart.GetCartId(context);
            return cart;
        }

        // Helper method to simplify shopping cart calls
        public static ShopCart GetCart(Controller controller)
        {
            return GetCart(controller.HttpContext);
        }

        // We're using HttpContextBase to allow access to cookies.
        public string GetCartId(HttpContextBase context)
        {
            if (context.Session[CartSessionKey] == null)
            {
                // Generate a new random GUID using System.Guid class
                Guid tempCartId = Guid.NewGuid();

                // Send tempCartId back to client as a cookie
                context.Session[CartSessionKey] = tempCartId.ToString();
            }

            return context.Session[CartSessionKey].ToString();
        }

        public void AddToCart(int id, int type)
        {
            // Get the matching cart and album instances
            var cartItem = connection.carts.SingleOrDefault(
            c => c.CartId == ShoppingCartId
            && c.TypeId == type
            && c.ProductId == id);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new carts
                {
                    TypeId = type,
                    ProductId = id,
                    CartId = ShoppingCartId,
                    Count = 1,
                    DateCreated = DateTime.Now
                };

                connection.AddTocarts(cartItem);
            }
            else
            {
                // If the item does exist in the cart, then add one to the quantity
                cartItem.Count++;
            }

            // Save changes
            connection.SaveChanges();
        }

        public int RemoveFromCart(int id)
        {
            /*
            // Get the cart
            var cartItem = storeDB.Carts.Single(
            cart => cart.CartId == ShoppingCartId
            && cart.RecordId == id);

            int itemCount = 0;

            if (cartItem != null)
            {
                if (cartItem.Count > 1)
                {
                    cartItem.Count--;
                    itemCount = cartItem.Count;
                }
                else
                {
                    storeDB.Carts.Remove(cartItem);
                }

                // Save changes
                storeDB.SaveChanges();
            }

            return itemCount;
            */
            return 0;
        }

        public void EmptyCart()
        {
            /*
            var cartItems = storeDB.Carts.Where(cart => cart.CartId == ShoppingCartId);

            foreach (var cartItem in cartItems)
            {
                storeDB.Carts.Remove(cartItem);
            }

            // Save changes
            storeDB.SaveChanges();
            */
        }

        public IQueryable<carts> GetCartItems()
        {
            var result = from p in connection.carts
                         where p.CartId == ShoppingCartId
                         orderby p.RecordId
                         select p;
            return result;
        }
        

        public int GetCount()
        {
            /*
            // Get the count of each item in the cart and sum them up
            int? count = (from cartItems in storeDB.Carts
                          where cartItems.CartId == ShoppingCartId
                          select (int?)cartItems.Count).Sum();

            // Return 0 if all entries are null
            return count ?? 0;
            */
            return 0;
        }

        public decimal GetTotal()
        {
            /*
            // Multiply album price by count of that album to get 
            // the current price for each of those albums in the cart
            // sum all album price totals to get the cart total
            decimal? total = (from cartItems in storeDB.Carts
                              where cartItems.CartId == ShoppingCartId
                              select (int?)cartItems.Count * cartItems.Album.Price).Sum();
            return total ?? decimal.Zero;
            */
            return decimal.Zero;
        }
    }
}